package br.webcrawler.activities.webcrawler.xml;


import br.webcrawler.activities.webcrawler.utils.Data;

import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

/**
 *
 * @author Gerson Groth
 */
public class Xml {
    private Element activities = new Element("Activities");
    private Element activity = null;
    private Element summary = null;
    private Element details;
    private Element timing;
    private Element elevation;
    private Element heartRate;
    private Element cadence;
    private Element weather;
    private Element additionalInfo;
    private Element laps;
    
    private Element allActivities = new Element("Activities");
    
    public void nextActivity(String file, String maxHR, String user){
         activity = new Element("Activity");
         activity.setAttribute("file", file);
         activity.setAttribute("maxHR", maxHR);
         activity.setAttribute("user", user);
         
         activities.addContent(activity.clone());
    }

    
    
    public void saveActivity(long i){
        if(activity != null){
            
           allActivities.addContent(activity.clone());
            
            Document doc = new Document();  
            doc.setRootElement(activity);  
            XMLOutputter xout = new XMLOutputter();  
            System.err.print(" ");
            
            try {  
                xout.setFormat(Format.getPrettyFormat().setEncoding("ISO-8859-1"));
                xout.output(doc, new FileWriter(Data.getInstance().getXmlFolder() + "activity_"+i+".xml"));
            } catch (IOException ex) {
//            	ex.printStackTrace();
                Logger.getLogger(Xml.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        
    }
    
    public void imprimeXML(){
        Document doc = new Document();  
        Element newActivity = activities.clone();
        newActivity.setAttribute("numberOfActivities", Integer.toString(newActivity.getChildren().size()));
        doc.setRootElement(newActivity);  
        
        XMLOutputter xout = new XMLOutputter();  
        try {  
            xout.setFormat(Format.getPrettyFormat());
            xout.output(doc, System.out);
            xout.output(doc, new FileWriter(Data.getInstance().getXmlFolder() + "activities.xml"));
        } catch (IOException ex) {
            Logger.getLogger(Xml.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
        doc = new Document(); 
       
        Element newAllActivity = allActivities.clone();
        newAllActivity.setAttribute("numberOfActivities", Integer.toString(newAllActivity.getChildren().size()));
        doc.setRootElement(newAllActivity);  

        xout = new XMLOutputter();  
        try {  
            xout.setFormat(Format.getPrettyFormat().setEncoding("ISO-8859-1"));
            xout.output(doc, new FileWriter(Data.getInstance().getXmlFolder() + "activitiesAll.xml"));
        } catch (IOException ex) {
            Logger.getLogger(Xml.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }  

    void addHeader(String activityName, String timestamp, String username, 
                    String activityTypeValue, 
                    String eventTypeValue, String courseValue) {
        Element header = new Element("HeaderContent");
        
        Element activityNameElement = new Element("ActivityName");
        activityNameElement.setText(activityName);
        
        Element timestampElement = new Element("Timestamp");
        timestampElement.setText(timestamp);
        
        Element userElement  = new Element("UserName");
        userElement.setText(username);
        
         Element activityType = new Element("ActivityType");
        activityType.setText(activityTypeValue);
        
         Element eventType = new Element("EventType");
        eventType.setText(eventTypeValue);
        
         Element course = new Element("Course");
        course.setText(courseValue);
        
        
        
        
        header.addContent(activityNameElement);
        header.addContent(timestampElement);
        header.addContent(userElement);
        header.addContent(activityType);
        header.addContent(eventType);
        header.addContent(course);
        activity.addContent(header);
    }

    public void createSummary(){
        summary = new Element("Summary");
        activity.addContent(summary);
    }
    
    public void addSummary(String name, String value){
        Element element = new Element(name);
        element.setText(value);
        summary.addContent(element);
    }

    void createDetails() {
        details = new Element("Details");
        timing = new Element("Timing");
        elevation = new Element("Elevation");
        heartRate = new Element("HeartRate");
        cadence = new Element("Cadence");
        
        
        details.addContent(timing);
        details.addContent(elevation);
        details.addContent(heartRate);
        details.addContent(cadence);
        activity.addContent(details);
    }
    
    public void addTiming(String name, String value){
        Element element = new Element(name);
        element.setText(value);
        timing.addContent(element);
    }

    void addElevation(String name, String value) {
        Element element = new Element(name);
        element.setText(value);
        elevation.addContent(element);
    }

    void addHeartHate(String name, String value) {
        Element element = new Element(name);
        element.setText(value);
        heartRate.addContent(element);
    }

    void addCadence(String name, String value) {
        Element element = new Element(name);
        element.setText(value);
        cadence.addContent(element);
    }

    void createWeather() {
        weather = new Element("Weather");
        activity.addContent(weather);
    }

    void addWeather(String name, String value) {
        Element element = new Element(name);
        element.setText(value);
        weather.addContent(element);
    }

    void createAdditionalInfo() {
        additionalInfo = new Element("AdditionalInfo");
        activity.addContent(additionalInfo);
    }

    void addAdditionalInfo(String name, String value) {
        Element element = new Element(name);
        element.setText(value);
        additionalInfo.addContent(element);
    }
    
    void createLaps(String nrLaps) {
        laps = new Element("Laps");
        laps.setAttribute("nrLaps", nrLaps);
        activity.addContent(laps);
    }

    void addLap(String split, String time, String distance, String avgPace) {
        Element lap = new Element("Lap");
        lap.setAttribute("split", split);
        lap.setAttribute("time", time);
        lap.setAttribute("distance", distance);
        lap.setAttribute("avgPace", avgPace);
        laps.addContent(lap);
    }
}
